<?php

include_once ("Device.class.php");


class WPNotification {
	private $account_id;
	private $devobj;

	function __construct ($AccountID) {
		$this->account_id = $AccountID;
		$this->devobj = new Device ();
	}

	public function SendRaw () {
		 // Create the toast message
		$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
                "<wp:Notification xmlns:wp=\"WPNotification\">" .
                   "<root>" .
                        "<Update>Yes</Update>" .
					"</root>" .
                "</wp:Notification>";

		$devices = $this->devobj->GetDevices ($this->AccountID);
		foreach ($devices as $device) {
			$this->_SentNotificationToMPNS ($device["URL"], "3", $xml);
		}
	}

	public function SendToast ($Text1, $Text2) {
		 // Create the toast message
		$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
                "<wp:Notification xmlns:wp=\"WPNotification\">" .
                   "<wp:Toast>" .
                        "<wp:Text1>" . $Text1 . "</wp:Text1>" .
                        "<wp:Text2>" . $Text2 . "</wp:Text2>" .
					"</wp:Toast>" .
                "</wp:Notification>";

		$devices = $this->devobj->GetDevices ($this->AccountID);
		foreach ($devices as $device) {
				$this->_SentNotificationToMPNS ($device["URL"], "2", $xml);
		}
	}

	public function SendTileUpdate () {
		// TODO
	}


	private function _SentNotificationToMPNS ($URL, $NotificationClass, $XmlContent) {
		// Create request to send
		$r = curl_init();
		curl_setopt($r, CURLOPT_URL, $URL);
		curl_setopt($r, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($r, CURLOPT_POST, true);
		curl_setopt($r, CURLOPT_HEADER, true);

		// Add headers
		$httpHeaders = Array ('Content-type: text/xml; charset=utf-8', 'Accept: application/*',
			'X-NotificationClass: {$NotificationClass}','Content-Length:' . strlen ($XmlContent));
		curl_setopt($r, CURLOPT_HTTPHEADER, $httpHeaders);

		// Add message
		curl_setopt($r, CURLOPT_POSTFIELDS, $XmlContent);

		// execute request
		$output = curl_exec ($r);
		curl_close($r);
	}
}
